package com.yimi.yinhepda.db.dao;

import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.JieLiuJianTable;
import com.pda.uce.commom.util.LogUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: lj
 * Date: 2019/11/16 16:56
 * Description:
 */
public class JieLiuJianTableDao extends BaseDao<JieLiuJianTable, Long> {

    public JieLiuJianTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), JieLiuJianTable.class);
    }

    public void createOrUpdate(List<JieLiuJianTable> list) {
        SQLiteDatabase database = getDbHelper().getWritableDatabase();
        String tableName = dao.getTableName();
        StringBuffer sql = new StringBuffer("replace into ");
        sql.append(tableName);
        sql.append("(billCode,");
        sql.append("nosPdaMark,");
        sql.append("operateStatus,");
        sql.append("createTime)");
        //5
        sql.append("values(?,?,?,?)");
        LogUtil.e("sql=" + sql.toString());

        database.beginTransaction();
        for (JieLiuJianTable item : list) {
            Object[] values = new Object[]{
                    item.getBillCode(), item.getNosPdaMark(),
                    item.getOperateStatus(), item.getCreateTime()
            };
            database.execSQL(sql.toString(), values);
        }
        database.setTransactionSuccessful();
        database.endTransaction();
    }

    public JieLiuJianTable queryWaybillNo(String waybillNo) {
        try {
            QueryBuilder<JieLiuJianTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("billCode", waybillNo);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
